package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.system.service.impl.SysInfoServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 系统信息Controller
 *
 * @author ZGL
 * @date 2025-03-10
 */
@RestController
@RequestMapping("/system/SysInfo")
public class SysInfoController extends BaseController
{
    //系统配置
    @Autowired
    private SysInfoServiceImpl sysInfoService;

    // 令牌有效期（默认30分钟）
    @Value("${token.expireTime}")
    private int expireTime;

    /**
     * 获取系统信息详细信息
     */
    @GetMapping
    public AjaxResult getSysInfo(HttpServletRequest request)
    {
        Map<String,Object> result = new HashMap<>();
        SysInfo sysInfo = sysInfoService.selectSysInfo();
        if(sysInfo!=null){
            result = JSONObject.parseObject(JSONObject.toJSONString(sysInfo), Map.class);
        }
        //获取服务器信息
        result.put("YSD_BASE_URL", sysInfoService.getYsdBaseUrl(request));
        //资源映射路径 前缀
        result.put("RESOURCE_PREFIX", Constants.RESOURCE_PREFIX + "/");

        if(sysInfo.getExpireTime() == null){
            result.put("expireTime", expireTime);
        }
        return AjaxResult.success(result);
    }

    /**
     * 获取系统信息详细信息
     */
    @GetMapping("/info")
    public AjaxResult getSysDescInfo(HttpServletRequest request)
    {
        Map<String,Object> result = new HashMap<>();
        SysInfo sysInfo = sysInfoService.selectSysInfo();
        if(sysInfo!=null){
            result.put("name",sysInfo.getName());
            result.put("logo",sysInfo.getLogo());
            result.put("loginBg",sysInfo.getLoginBg());
            result.put("loginBoxLocation",sysInfo.getLoginBoxLocation());
        }
        return AjaxResult.success(result);
    }

    /**
     * 新增系统信息
     */
    @Log(title = "系统信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysInfo sysInfo)
    {
        return toAjax(sysInfoService.saveSysInfo(sysInfo));
    }

    /**
     * Logo上传
     */
    @Log(title = "平台Logo", businessType = BusinessType.UPDATE)
    @PostMapping("/logo")
    public AjaxResult logo(@RequestParam("logofile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String logo = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (sysInfoService.saveLogo(logo))
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("logoUrl", logo);
                return ajax;
            }
        }
        return error("上传图片异常，请联系管理员");
    }

    /**
     * 登录页背景图上传
     */
    @Log(title = "平台Logo", businessType = BusinessType.UPDATE)
    @PostMapping("/loginBg")
    public AjaxResult loginBg(@RequestParam("loginBgFile") MultipartFile file,
                              @RequestParam(value = "location",required = false) String location) throws Exception
    {
        if (!file.isEmpty())
        {
            String loginBgUrl = FileUploadUtils.upload(RuoYiConfig.getLoginBgPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (sysInfoService.saveloginBg(loginBgUrl,location))
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("loginBgUrl", loginBgUrl);
                return ajax;
            }
        }
        return error("上传图片异常，请联系管理员");
    }
}
